package com.googlecode.xmltojava;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Item {
	public static final String T_EVENT = "VEVENT";
	public static final String T_TODO  = "VTODO";
	public static final String T_JOURNAL = "VJOURNAL";
	
	
	public static final int F_SUMMARY = 0;
	public static final int F_TITLE = 1;
	public static final int F_DESCRIPTION = 2;
	public static final int F_ORGANIZER = 3;
	
	private String _title;
	private String _summary;
	private String _description;
	private String _organizer;
	private Date _date;
	private Date _dateend;
	private Date _datestamp;
	private String _type;
	private String _status;
	
	private SimpleDateFormat sdf;
	static public final String dateFormat = "dd/MM/yyyy HH:mm";
	static public final String shortDateFormat = "dd/MM/yyyy";

	public Item() {
		sdf = new SimpleDateFormat(dateFormat);
		_title = "";
		_summary = "";
		_date = null;
		_dateend = null;
	}
	Item(String title, String summary, Date d) {
		sdf = new SimpleDateFormat(dateFormat);
		_title = title;
		_summary = summary;
		_date = d;			
	}
	Item(String title, String summary, Date d, Date dend) {
		sdf = new SimpleDateFormat(dateFormat);
		_title = title;
		_summary = summary;
		_date = d;
		_dateend = dend;
	}
	public boolean hasDate(Calendar c) {
		return hasDate(c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.MONTH), c.get(Calendar.YEAR));
	}
	public boolean hasDate(int day, int month, int year) {
		int _day, _month, _year;
		Calendar c = new GregorianCalendar();
		//Se Event, ho DStart e DEnd
		//Se Journal, ho DStamp
		//Se Todo, ho DEnd
		
		if (_type.equals(T_EVENT)) {
			c.set(year, month, day, 12, 0, 0);
			
			Calendar cstart = new GregorianCalendar();
			Calendar cend = new GregorianCalendar();
			cstart.setTime(_date);
			cend.setTime(_dateend);
			
			cstart.set(cstart.get(Calendar.YEAR), cstart.get(Calendar.MONTH), cstart.get(Calendar.DAY_OF_MONTH), 0, 0, 1);
			cend.set(cend.get(Calendar.YEAR), cend.get(Calendar.MONTH), cend.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
			
			if (!cstart.after(c) && !cend.before(c))
				return true;
			return false;
		} else {
			if (_type.equals(T_TODO)) {
				if (_dateend != null)
					c.setTime(_dateend);
				else
					c.setTime(_datestamp);
			} else
				c.setTime(_datestamp);
		
			_day = c.get(Calendar.DAY_OF_MONTH);
			_month = c.get(Calendar.MONTH);
			_year = c.get(Calendar.YEAR);
			if (_day == day && _month == month && _year == year)		
				return true;
			return false;
		}		
	}
	public boolean isAfter(int day, int month, int year) {
		Calendar c = new GregorianCalendar();
		if (_type.equals(T_EVENT))
			c.setTime(_dateend);
		else if (_type.equals(T_JOURNAL))
			c.setTime(_datestamp);
		else 
			c.setTime(_dateend);
		
		Calendar cu = new GregorianCalendar();
		cu.set(year, month, day);
		
		return c.after(cu);
	}
	public boolean isAfter(Calendar c) {
		return isAfter(c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.MONTH), c.get(Calendar.YEAR));
	}
	public boolean isBefore(int day, int month, int year) {
		Calendar c = new GregorianCalendar();
		if (_type.equals(T_EVENT))
			c.setTime(_dateend);
		else if (_type.equals(T_JOURNAL))
			c.setTime(_datestamp);
		else 
			c.setTime(_dateend);
		
		Calendar cu = new GregorianCalendar();
		cu.set(year, month, day);
		
		return c.before(cu);
	}
	public boolean isBefore(Calendar c) {
		return isBefore(c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.MONTH), c.get(Calendar.YEAR));
	}
	public boolean respects(String regex, int param) {
		if (param == F_TITLE) 
			return _title.matches(regex);
		else if (param == F_DESCRIPTION)
			return _description.matches(regex);
		else if (param == F_ORGANIZER)
			return _organizer.matches(regex);
		else if (param == F_SUMMARY)
			return _summary.matches(regex);
		else
			return false;
	}
	public boolean respects(String regex) {
		return respects(regex, F_TITLE);
	}
	public String toString() {
		if (_type == T_EVENT)
			return "[E] " + _summary;
		if (_type == T_JOURNAL)
			return "[J] " + _summary;
		if (_type == T_TODO)
			return "[T] " + _summary;
		return "";
	}
	
	public String get_title() {
		return _title;
	}
	public void set_title(String _title) {
		this._title = _title;
	}
	
	public String get_summary() {
		return _summary;
	}
	public void set_summary(String _summary) {
		this._summary = _summary;
	}
	
	public String get_description() {
		return _description;
	}
	public void set_description(String _description) {
		this._description = _description;
	}
	
	public String get_organizer() {
		return _organizer;
	}
	public void set_organizer(String _organizer) {
		this._organizer = _organizer;
	}
	
	public String get_status() { 
		return _status; 
	}
	public void set_status(String status) { 
		_status = status; 
	}
	
	public Date get_date() {
		return _date;
	}
	public void set_date(Date _date) {
		this._date = _date;
	}
	public void set_datex(String date) throws ParseException  {
		if (date.length()>0 && !date.equals("null"))
			this._date = sdf.parse(date);
	}
	
	public Date get_dateend() {
		return _dateend;
	}
	public void set_dateend(Date _dateend) {
		this._dateend = _dateend;
	}
	public void set_dateendx(String date) throws ParseException {
		if (date.length()>0)
			this._dateend = sdf.parse(date);
	}
	
	public Date get_datestamp() {
		return _datestamp;
	}
	public void set_datestamp(Date _datestamp) {
		this._datestamp = _datestamp;
	}
	public void set_datestampx(String date) throws ParseException {
		if (date.length()>0)
			this._datestamp = sdf.parse(date);
	}
	
	public String get_type() {
		return _type;
	}
	public void set_type(String _type) {
		this._type = _type;
	}
	
	@SuppressWarnings("deprecation")
	public int get_SHour() {
		if (_type == Item.T_EVENT) return _date.getHours();
		else if (_type == Item.T_JOURNAL) return _datestamp.getHours();
		else return _dateend.getHours();
	}
	@SuppressWarnings("deprecation")
	public int get_EHour() {
		if (_type == Item.T_EVENT) return _dateend.getHours();
		else if (_type == Item.T_JOURNAL) return _datestamp.getHours();
		else return _dateend.getHours();
	}
}
